<?php

/**
 * @author CHUN Dany <chundany@gmail.com>
 */
class Mod_Ads extends CI_Model
{ 
  public function getAds($start, $num, $q)
  {
    $this->db->select('a.*')
        ->select('CONCAT(u.'.field('lname').'," ",u.'.field('fname').') AS '.field('user'), FALSE)
        ->select('c.'.field('name').' AS '.field('category'))
        ->select('t.'.field('name').' AS '.field('type'))
        ->select('l.'.field('name').' AS '.field('location'))
        ->select('i.'.field('image'))
        ->from(table('ads').' a')
        ->join(table('user').' u', 'u.'.field('id').'=a.'.field('userId'))
        ->join(table('category').' c', 'c.'.field('id').'=a.'.field('categoryId'))
        ->join(table('type').' t', 't.'.field('id').'=a.'.field('typeId'))
        ->join(table('location').' l', 'l.'.field('id').'=a.'.field('locationId'))
        ->join(table('image').' i', 'i.'.field('adsId').'=a.'.field('id').' AND i.'.field('isDefault').'=1', 'left')
        ->where('a.'.field('deletedAt'), NULL)
        ->where('u.'.field('deletedAt'), NULL)
        ->order_by('a.'.field('updatedAt'), 'DESC');
        
    $this->db->limit($num, $start);
    return $this->db->get();
  }
  
  public function getAdsNum($q)
  {
    $objDb = $this->getAllAds($q);
    return $objDb->num_rows();
  }
  
  public function getAllAds($q)
  {
    $this->db->select('a.*')
        ->select('CONCAT(u.'.field('lname').'," ",u.'.field('fname').') AS '.field('user'), FALSE)
        ->select('c.'.field('name').' AS '.field('category'))
        ->select('t.'.field('name').' AS '.field('type'))
        ->select('l.'.field('name').' AS '.field('location'))
        ->select('i.'.field('image'))
        ->from(table('ads').' a')
        ->join(table('user').' u', 'u.'.field('id').'=a.'.field('userId'))
        ->join(table('category').' c', 'c.'.field('id').'=a.'.field('categoryId'))
        ->join(table('type').' t', 't.'.field('id').'=a.'.field('typeId'))
        ->join(table('location').' l', 'l.'.field('id').'=a.'.field('locationId'))
        ->join(table('image').' i', 'i.'.field('adsId').'=a.'.field('id').' AND i.'.field('isDefault').'=1', 'left')
        ->where('a.'.field('deletedAt'), NULL)
        ->where('u.'.field('deletedAt'), NULL)
        ->order_by('a.'.field('updatedAt'), 'DESC');
          
    return $this->db->get();
  }
  
  public function getAdsById($id)
  {
    $this->db->select('a.*')
        ->select('CONCAT(u.'.field('lname').'," ",u.'.field('fname').') AS '.field('user'), FALSE)
        ->select('c.'.field('name').' AS '.field('category'))
        ->select('t.'.field('name').' AS '.field('type'))
        ->select('l.'.field('name').' AS '.field('location'))
        ->select('i.'.field('image'))
        ->from(table('ads').' a')
        ->join(table('user').' u', 'u.'.field('id').'=a.'.field('userId'))
        ->join(table('category').' c', 'c.'.field('id').'=a.'.field('categoryId'))
        ->join(table('type').' t', 't.'.field('id').'=a.'.field('typeId'))
        ->join(table('location').' l', 'l.'.field('id').'=a.'.field('locationId'))
        ->join(table('image').' i', 'i.'.field('adsId').'=a.'.field('id').' AND i.'.field('isDefault').'=1', 'left')
        ->where('a.'.field('deletedAt'), NULL)
        ->where('u.'.field('deletedAt'), NULL)
        ->where('a.'.field('id'), $id);
        
    $result = $this->db->get();
    return $result->row_array();
  }
  
  public function getCategoryList()
  {
    $this->db->select(field('id').', '.field('name'));
    $this->db->from(table('category'));
    $this->db->where(field('isDisable'), 0);
    $this->db->where(field('parentId'), 0);
    $this->db->order_by(field('name'), 'ASC');
    $result = $this->db->get();
    
    $return = array('' => '');
    if($result->num_rows() > 0)
    {
      foreach($result->result_array() as $row)
      {
        $return[$row[field('id')]] = $row[field('name')];
        
        $child = $this->db->select(field('id').','.field('name'))->from(table('category'))->where(field('parentId'), $row[field('id')])->order_by(field('name'))->get();
        foreach($child->result_array() as $rows)
        {
          $return[$rows[field('id')]] = nbs(1).' &ndash; '.$rows[field('name')];
        }
      }
    }

    return $return;
  }
  
  public function getTypeList()
  {
    $this->db->select(field('id').', '.field('name'));
    $this->db->from(table('type'));
    $this->db->where(field('isDisable'), 0);
    $this->db->order_by(field('name'), 'ASC');
    $result = $this->db->get();
    
    $return = array('' => '');
    if($result->num_rows() > 0)
    {
      foreach($result->result_array() as $row)
      {
        $return[$row[field('id')]] = $row[field('name')];
      }
    }

    return $return;
  }
  
  public function getLocationList()
  {
    $this->db->select(field('id').', '.field('name'));
    $this->db->from(table('location'));
    $this->db->order_by(field('name'), 'ASC');
    $result = $this->db->get();
    
    $return = array('' => '');
    if($result->num_rows() > 0)
    {
      foreach($result->result_array() as $row)
      {
        $return[$row[field('id')]] = $row[field('name')];
      }
    }

    return $return;
  }
  
  public function insert()
  {
    $disable  = $this->input->post('disable', false);
    $data = array(
      field('title')            => $this->input->post('title'), 
      field('price')            => $this->input->post('price'),
      field('description')      => $this->input->post('description'),
      field('shortDescription') => $this->input->post('shortDescription'),
      field('userId')           => $this->session->userdata('userId'),
      field('categoryId')       => $this->input->post('categoryId'),
      field('publishedAt')      => dateFormat($this->input->post('publishDate'), 'Y-m-d'),
      field('typeId')           => $this->input->post('typeId'),
      field('locationId')       => $this->input->post('locationId'),
      field('isDisable')        => ($disable == 1 ? 1 : 0)
    );
    $this->db->set(field('updatedAt'), 'NOW()', FALSE);
    
    $this->db->insert(table('ads'), $data); 
    return $this->db->insert_id();
  }
  
  public function update($id)
  {
    $disable  = $this->input->post('disable', false);
    $data = array(
      field('title')            => $this->input->post('title'), 
      field('price')            => $this->input->post('price'),
      field('description')      => $this->input->post('description'),
      field('shortDescription') => $this->input->post('shortDescription'),
      field('userId')           => $this->session->userdata('userId'),
      field('categoryId')       => $this->input->post('categoryId'),
      field('publishedAt')      => dateFormat($this->input->post('publishDate'), 'Y-m-d'),
      field('typeId')           => $this->input->post('typeId'),
      field('locationId')       => $this->input->post('locationId'),
      field('isDisable')          => ($disable == 1 ? 1 : 0)
    );
    $this->db->set(field('updatedAt'), 'NOW()', FALSE);
          
    $this->db->where(field('id'), $id);
    $this->db->update(table('ads'), $data); 
    return $this->db->affected_rows();
  }
  
  public function delete($id)
  {
    $this->db->set(field('deletedAt'), 'NOW()', FALSE);
    $this->db->where(field('id'), $id);
    $this->db->update(table('ads'));
    return $this->db->affected_rows();
  }

  // ---------------------
  public function insertImage($name, $adsId){
    $data = array(
        field('image') => $name,
        field('adsId') => $adsId
      );
    $result = $this->db->insert(table('image'), $data);
    return $this->db->insert_id();
  }
  
  public function updateAdsImageDefault($id, $imageId){
    $data = array(
      field('imageId') => $imageId
    );
      
    $this->db->where(field('id'),$id);
    $result = $this->db->update(table('ads'), $data); 
    return $this->db->affected_rows();
  }
}